#include <am.h>
#include <npc.h>
#define SYNC_ADDR (VGACTL_ADDR + 4)

void __am_gpu_init() {
				 int i;
				 int w = io_read(AM_GPU_CONFIG).width;  
				 int h = io_read(AM_GPU_CONFIG).height;
				uint32_t *fb = (uint32_t *)(uintptr_t)FB_ADDR;
				for (i = 0; i < w * h; i ++) fb[i] = 0;
				outl(SYNC_ADDR, 1);
}

void __am_gpu_config(AM_GPU_CONFIG_T *cfg) {
  *cfg = (AM_GPU_CONFIG_T) {
    .present = true, .has_accel = false,
    .width = inl(VGACTL_ADDR)>>16, .height = inl(VGACTL_ADDR)&0xFFFF,
    .vmemsz = 0 
  };
}

void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *ctl) {
	int w = io_read(AM_GPU_CONFIG).width;  
	uint32_t *fb = (uint32_t *)(uintptr_t)FB_ADDR;
	uint32_t *pt= (uint32_t *)(uintptr_t)ctl->pixels;
	for(int i=0;i<ctl->h;i++){
		for(int j=0;j<ctl->w;j++){
			fb[(ctl->y+i)*w+ctl->x+j] =pt[i*ctl->w+j];
		}
	}
  if (ctl->sync) {
    outl(SYNC_ADDR, 1);
  }
}

void __am_gpu_status(AM_GPU_STATUS_T *status) {
  status->ready = true;
}
